package org.nggen.config;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Logger;

public class GenConfig {

	private static Logger logger = Logger.getLogger(GenConfig.class);

	private Pattern pattern = Pattern.compile("[{{](\\w+)[}}]");

	private Map<String, Object> map = new HashMap<String, Object>();

	private String controllerPath;
	private String servicePath;
	private String moduleViewPath;
	private String moduleCssPath;
	private String directivePath;
	private String[] directiveJsPath;
	private String[] directiveViewPath;

	private String basePath;
	private String moduleName;
	private boolean hasService;
	private boolean hasCss;
	private String[] directiveName;

	public GenConfig(String properties) {
		Prop me = PropKit.use(properties);

		map.put("controller_path", me.get("controller_path"));
		map.put("service_path", me.get("service_path"));
		map.put("module_view_path", me.get("module_view_path"));
		map.put("module_css_path", me.get("module_css_path"));
		map.put("directive_path", me.get("directive_path"));
		map.put("directive_js_path", me.get("directive_js_path"));
		map.put("directive_view_path", me.get("directive_view_path"));
		map.put("base_path", me.get("base_path"));
		map.put("module_name", me.get("module_name"));
		// map.put("has_service", me.get("has_service"));
		map.put("directive_name", me.get("directive_name"));

		parse(map, null);

		controllerPath = (String) map.get("controller_path");
		servicePath = (String) map.get("service_path");
		moduleCssPath = (String) map.get("module_css_path");
		moduleViewPath = (String) map.get("module_view_path");
		directivePath = (String) map.get("directive_path");

		basePath = (String) map.get("base_path");
		moduleName = (String) map.get("module_name");
		hasService = me.getBoolean("has_service");
		hasCss = me.getBoolean("has_css");

		String drcName = (String) map.get("directive_name");
		String drcJsPath = (String) map.get("directive_js_path");
		String drcViewPath = (String) map.get("directive_view_path");
		directiveName = drcName.split(",");
		directiveJsPath = new String[directiveName.length];
		directiveViewPath = new String[directiveName.length];
		for (int i = 0; i < directiveName.length; i++) {
			String name = directiveName[i];
			String jsPath = drcJsPath.replaceAll(drcName, name);
			String viewPath = drcViewPath.replaceAll(drcName, name);
			directiveJsPath[i] = jsPath;
			directiveViewPath[i] = viewPath;
		}
	}

	private String parse(Map<String, Object> map, String key) {
		String _value = null;
		if (key == null) {
			for (String _key : map.keySet()) {
				parse(map, _key);
			}
		} else if (!key.trim().equals("")) {
			if (map.containsKey(key)) {
				Object value = map.get(key);
				if (value != null) {
					Matcher matcher = pattern.matcher(value.toString());
					while (matcher.find()) {
						String subkey = matcher.group(1);
						String subvalue = parse(map, subkey);

						value = value.toString().replaceAll("\\{\\{" + subkey + "\\}\\}", subvalue);
					}
					map.put(key, value);
					_value = value.toString();
				}
			} else {
				logger.error(key + " not exists!");
			}
		}
		return _value;
	}

	public String getControllerPath() {
		return controllerPath;
	}

	public String getServicePath() {
		return servicePath;
	}

	public String getModuleViewPath() {
		return moduleViewPath;
	}

	public String getModuleCssPath() {
		return moduleCssPath;
	}

	public String getDirectivePath() {
		return directivePath;
	}

	public String[] getDirectiveJsPath() {
		return directiveJsPath;
	}

	public String[] getDirectiveViewPath() {
		return directiveViewPath;
	}

	public String getBasePath() {
		return basePath;
	}

	public String getModuleName() {
		return moduleName;
	}

	public boolean isHasService() {
		return hasService;
	}

	public boolean isHasCss() {
		return hasCss;
	}

	public String[] getDirectiveName() {
		return directiveName;
	}

	public static void main(String[] args) {
		// Pattern pattern = Pattern.compile("[{{](\\w+)[}}]");
		// Matcher matcher =
		// pattern.matcher("{{base_path}}/{{module_name}}/js/controller");
		// int i = 1;
		// while(matcher.find()){
		// System.out.println(matcher.group(1));
		// matcher.replaceAll(i + "");
		// i++;
		// }

	}

}
